<template>
    <div class="goods-comment">
        <div class="head" v-if="commentInfo">
            <div class="data">
                <p>
                    <span>{{ commentInfo.salesCount }}</span><span>人购买</span>
                </p>
                <p>
                    <span>{{ commentInfo.praisePercent }}</span><span>好评率</span>
                </p>
                <p>
                    <span>{{ commentInfo.evaluateCount }}</span><span>条评价</span>
                </p>
                <p>
                    <span>{{ commentInfo.hasPictureCount}}</span>
                    <span>条有图评价</span>
                </p>
            </div>
        </div>
        <!-- 排序 -->
        <div class="sort">
            <span>排序：</span>
            <a @click="changeSort(null)" href="javascript:;" :class="{ active: sortField === null }">默认</a>
            <a @click="changeSort('praiseCount')" href="javascript:;"
                :class="{ active: sortField === 'praiseCount' }">最热</a>
            <a @click="changeSort('createTime')" href="javascript:;"
                :class="{ active: sortField === 'createTime' }">最新</a>
            <XtxCheckbox v-model="hasPicture" style="margin-left:50px;">是否有图</XtxCheckbox>
        </div>
        <!-- 列表 -->
        <div class="list">
            <div class="item" v-for="item in commentList" :key="item.id">
                <div class="user">
                    <img :src="item.member.avatar" alt="" />
                    <span>{{ item.member.nickname }}</span>
                </div>
                <div class="body">
                    <div class="score">
                        <!-- 实心 -->
                        <i v-for="i in item.score" :key="i + '1'" class="iconfont icon-wjx01"></i>
                        <!-- 空心 -->
                        <i v-for="i in 5 - item.score" :key="i + '2'" class="iconfont icon-wjx02"></i>
                        <span class="attr">{{ item.orderInfo.specsText }}</span>
                    </div>
                    <div class="text">{{ item.content }}</div>
                    <!-- 使用图片预览组件 -->
                    <GoodsCommentImage v-if="item.pictures.length" :pictures="item.pictures" />
                    <div class="time">
                        <span>{{ item.createTime }}</span>
                        <span class="zan"><i class="iconfont icon-dianzan"></i>
                            {{ item.praiseCount }}</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { findCommentInfoByGoods, findCommentListByGoods } from '@/api/goods'
import { onMounted, ref, reactive, toRefs, watch } from 'vue'
import { useRoute } from 'vue-router'
import GoodsCommentImage from '../goods-comment-image'

export default {
  name: 'GoodsComment',
  components: {
    GoodsCommentImage
  },
  setup () {
    const { commentInfo } = useCommentInfo()
    const currTagIndex = ref(0)
    const changeTag = (i) => {
      currTagIndex.value = i
    }
    const { reqParams, commentList, changeSort } = useCommentList()
    return { commentInfo, currTagIndex, changeTag, ...toRefs(reqParams), commentList, changeSort }
  }
}

// 抽离逻辑的时候把异步封装到里面,避免顶层函数使用async
function useCommentInfo () {
  const commentInfo = ref(null)
  const route = useRoute()
  const getCommentInfo = async () => {
    const data = await findCommentInfoByGoods(route.params.id)
    commentInfo.value = data.result
  }
  onMounted(() => {
    getCommentInfo()
  })
  return { commentInfo }
}

// 获取基础评论数据
function useCommentList () {
  const route = useRoute()
  // 筛选条件准备
  const reqParams = reactive({
    page: 1,
    pageSize: 10,
    hasPicture: false, // 是否右图
    sortField: null // 排序
  })
  // 切换排序
  function changeSort (field) {
    reqParams.sortField = field
  }
  watch(() => {
    return reqParams
  }, () => {
    loadCommentList()
  }, { deep: true })

  // 评价列表
  const commentList = ref([])
  async function loadCommentList () {
    const data = await findCommentListByGoods(route.params.id, reqParams)
    commentList.value = data.result.items
  }
  onMounted(async () => {
    loadCommentList()
  })

  return { commentList, changeSort, reqParams }
}

</script>
<style scoped lang="scss">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      flex:1;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: $priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: $xtxColor;
      }
    }
  }
}
.list {
  padding: 0 20px;
  .item {
    display: flex;
    padding: 25px 10px;
    border-bottom: 1px solid #f5f5f5;
    .user {
      width: 160px;
      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        overflow: hidden;
      }
      span {
        padding-left: 10px;
        color: #666;
      }
    }
    .body {
      flex: 1;
      .score {
        line-height: 40px;
        .iconfont {
          color: #ff9240;
          padding-right: 3px;
        }
        .attr {
          padding-left: 10px;
          color: #666;
        }
      }
    }
    .text {
      color: #666;
      line-height: 24px;
    }
    .time {
      color: #999;
      display: flex;
      justify-content: space-between;
      margin-top: 5px;
    }
  }
}
</style>
